package scau.hotel.hotel_web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import scau.hotel.hotel_service.entity.CheckinCusInfor;
import scau.hotel.hotel_service.service.CheckinCusInforService;
import scau.hotel.hotel_model.controller.RespBean;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CheckinCusInfor)表控制层
 *
 * @author makejava
 * @since 2022-03-28 20:30:26
 */
@RestController
@PreAuthorize("hasAnyAuthority('admin','normal')")
@RequestMapping("checkinCusInfor")
@Api(value = "typeApi", tags = "入住人员控制器")
public class CheckinCusInforController {
    /**
     * 服务对象
     */
    @Resource
    private CheckinCusInforService checkinCusInforService;

    /**
     * 不分页查询
     *
     * @param checkinCusInfor 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    @ApiOperation("不分页查询所有")
    public RespBean queryAll(CheckinCusInfor checkinCusInfor) {
        return RespBean.success(this.checkinCusInforService.queryAll(checkinCusInfor));
    }

    /**
     * 分页查询
     *
     * @param pageNum  起始页
     * @param pageSize 每页数据大小
     * @param checkinCusInfor     筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    @ApiOperation("分页查询所有数据")
    public RespBean page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, CheckinCusInfor checkinCusInfor) {
        PageHelper.startPage(pageNum, pageSize);
        List<CheckinCusInfor> list = checkinCusInforService.queryAll(checkinCusInfor);
        PageInfo<List> pageInfo = new PageInfo(list);
        return RespBean.success(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param checkinCusInforId 主键
     * @return 单条数据
     */
    @GetMapping("{checkinCusInforId}")
    @ApiOperation("根据id查询数据")
    public RespBean queryById(@PathVariable("checkinCusInforId") Integer checkinCusInforId) {
        return RespBean.success(this.checkinCusInforService.queryById(checkinCusInforId));
    }

    /**
     * 通过IdCard查询单条数据
     *
     * @param idCard 查询条件
     * @return 单条数据
     */
    @GetMapping("/queryByIdCard/{idCard}")
    @ApiOperation("根据身份证查询数据")
    public RespBean queryByIdCard(@PathVariable("idCard") String idCard) {
        return RespBean.success(this.checkinCusInforService.queryByIdCard(idCard));
    }

    /**
     * 通过Name查询单条数据
     *
     * @param cusName 查询条件
     * @return 单条数据
     */
    @GetMapping("/queryByName/{cusName}")
    @ApiOperation("根据姓名查询数据")
    public RespBean queryByName(@PathVariable("cusName") String cusName) {
        return RespBean.success(this.checkinCusInforService.queryByName(cusName));
    }

    /**
     * 新增数据
     *
     * @param checkinCusInfor 实体
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增数据")
    public RespBean add(@RequestBody CheckinCusInfor checkinCusInfor) {
        return this.checkinCusInforService.insert(checkinCusInfor);
    }

    /**
     * 编辑数据
     *
     * @param checkinCusInfor 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation("编辑数据")
    public RespBean edit(@RequestBody CheckinCusInfor checkinCusInfor) {
        return this.checkinCusInforService.update(checkinCusInfor);
    }

    /**
     * 删除数据
     *
     * @param checkinCusInforId 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete/{checkinCusInforId}")
    @ApiOperation("根据id删除数据")
    @PreAuthorize("hasAuthority('admin')")
    public RespBean deleteById(@PathVariable Integer checkinCusInforId) {
        CheckinCusInfor checkinCusInfor = new CheckinCusInfor();
        checkinCusInfor.setCheckinCusInforId(checkinCusInforId);
        checkinCusInfor.setDelFlag(0);
        return this.checkinCusInforService.update(checkinCusInfor);
    }


}

